﻿@using SRL_Statistics.Services;
@using System.Collections.Specialized;

@{
    if (IsPost)
    {
        DBAccessor accessor = new DBAccessor();
        string scriptName = Request.Form.Get("scriptName");
        string password = Request.Form.Get("passwordText");
        int varCount = Request.Form.Get("varCount").AsInt(0);
        if (Request.Form.Get("variable0") != null)
        {
            List<String> variables = new List<string>();
            foreach (String str in Request.Form.AllKeys)
            {
                if (str.StartsWith("variable"))
                {
                    variables.Add(Request.Form.Get(str));
                }
            }
            if (accessor.RegisterScript(scriptName, password, variables)) 
            { 
                <p>Congratulations!  You successfully registered!</p>
            }
            else
            {
                <p>A final unexpected error occured.  Please report this to Kevin and include all details on what you attempted to add.</p>
            }
        }
        else {
            if (!accessor.RegisterScript(scriptName, password))
            {
                <p>It looks like we can't register your script, was the name already taken by someone else?</p>
                using (Html.BeginForm("Add", "Script"))
                {
                    @Html.Label("scriptLabel", "Script Name: ");
                    @Html.TextBox("scriptName", scriptName);
                    <br />
                    @Html.Label("passwordLabel", "Password Text: ");
                    @Html.TextBox("passwordText", password);
                    <br />
                    @Html.Label("variableLabel", "Variable Count: ");
                    @Html.TextBox("varCount", varCount);
                    <br />
                    <input type="submit" id="Submit" value="Submit" />
                }
            }
            else
            {
                using (Html.BeginForm("Add", "Script"))
                {
                    @Html.Label("scriptLabel", "Script Name: ");
                    @Html.TextBox("scriptName", scriptName);
                    <br />
                    @Html.Label("passwordLabel", "Password Text: ");
                    @Html.TextBox("passwordText", password);
                    <br />
                    for (int i = 0; i < varCount; i++)
                    {
                        @Html.Label("variableLabel" + i, "Variable " + i + ": ");
                        @Html.TextBox("variable" + i);
                        <br />
                    }
                    <input type="submit" id="Submit" value="Submit" />
                }
            }
        }
    }
    else
    {
        using (Html.BeginForm("Add", "Script"))
        {
            @Html.Label("scriptLabel", "Script Name: ");
            @Html.TextBox("scriptName");
            <br />
            @Html.Label("passwordLabel", "Password Text: ");
            @Html.TextBox("passwordText");
            <br />
            @Html.Label("variableLabel", "Variable Count: ");
            @Html.TextBox("varCount");
            <br />
            <input type="submit" id="Submit" value="Submit" />
        }
    }
}